// subPages/shopPage/addressManager/index.js
var app = getApp();
var utils = require('../../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:'',
    isSelect:true

  },
  //选择地址
  selAddress(e){
      var addressId = e.currentTarget.dataset.id;
      console.log(addressId);
      wx.setStorageSync('addressId',addressId);
      this.onLoad();
      setTimeout(function () {
          wx.navigateBack({
              delta: 1
          })
      },500)

  },
  delAddress(e){
    var that = this;
    var addressId = e.currentTarget.dataset.id;
    wx.showModal({
        title:'提示',
        content:'是否删除该地址？',
        success(res){
            if (res.confirm) {
                utils.sendAjax(app.globalData.apiUrl+'/delete_address','get',function(res){
                    console.log(res);
                    if(res.data==1){
                        wx.showToast({
                            title:'地址已删除！',
                            icon:'none',
                            duration:1000
                        })
                        setTimeout(function(){
                            that.onShow();
                        },1000)
                    }
                },{id:addressId})
            } else if (res.cancel) {
                console.log('用户点击取消')
            }
        }
    })
  },
  setMoren(e){
      var that = this;
      var addressId = e.currentTarget.dataset.id;
      utils.sendAjax(app.globalData.apiUrl+'/moren_address','GET',function(res){
          console.log(res);
          if(res.data == 1){
            that.onShow();
          }
      },{
        id:addressId,
        userid:app.globalData.userId
      })
  },
    addAddress(){
      wx.navigateTo({
          url:'../addAddress/addAddress'
      })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      var that = this;
      var addressId = wx.getStorageSync('addressId');
      that.setData({
          addressId:addressId
      })
      utils.sendAjax(app.globalData.apiUrl+'/user_get_address','GET',function(res){
          console.log(res);
          that.setData({
              address:res.data
          })
      },{userid:app.globalData.userId})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
